/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.model;

import lombok.Getter;
import lombok.Setter;
import org.snaker.engine.Action;
import org.snaker.engine.interceptors.SnakerInterceptor;
import org.snaker.engine.core.Execution;
import org.snaker.engine.handlers.impl.CreateTaskHandler;
import org.snaker.engine.handlers.impl.StartSubProcessHandler;

import java.util.List;

/**
 * 节点连接线
 * @author yuqs
 * @since 1.0
 */
@Getter
@Setter
public class TransitionModel extends BaseModel implements Action {

	/**
	 *
	 */
	private static final long serialVersionUID = 3688123410411321158L;
	/**
	 * 变迁的源节点引用
	 */
	private NodeModel source;
	/**
	 * 变迁的目标节点引用
	 */
	private NodeModel target;
	/**
	 * 变迁的目标节点name名称
	 */
	private String to;
	/**
	 * 变迁的条件表达式，用于decision
	 */
	private String expr;
	/**
	 * 连线上每个转折点上的坐标数据顺序路径相反
	 */
	private String g;
	/**
	 * 连线上的文字的相对便宜坐标x,y
	 */
	private String offset;
	/**
	 * 当前连线是否可用
	 */
	private boolean enabled = false;


	public void execute(Execution execution, List<SnakerInterceptor> interceptors) {
		if(!enabled) return;
		if(target instanceof TaskModel) {
			//如果目标节点模型为TaskModel，则创建task
			fire(new CreateTaskHandler((TaskModel)target), execution, interceptors);
		} else if(target instanceof SubProcessModel) {
			//如果目标节点模型为SubProcessModel，则启动子流程
			fire(new StartSubProcessHandler((SubProcessModel)target), execution, interceptors);
		} else {
			//如果目标节点模型为其它控制类型，则继续由目标节点执行
			target.execute(execution, interceptors);
		}
	}
}
